package com.itheima.boottlias.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 统计方法时长的切面
 * @author ghy
 * @version 1.0
 * @since 2025-07-18 08:45
 */
@Slf4j
// @Aspect   //表示当前类是一个切面类
// @Component
public class RecordTimeAspect {

    // 提取切点表达式
    @Pointcut("execution(* com.itheima.boottlias.service.impl.DeptServiceImpl.*(..))")
    public void pt(){}

    @Around("pt()")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        // 记录当前时间
        long start = System.currentTimeMillis();
        Object result = pjp.proceed();
        // 记录当前时间
        long end = System.currentTimeMillis();
        // 统计时长
        log.warn("耗时{}ms", end-start);
        return result;
    }

    @Before("pt()")
    public void beforeMethod(JoinPoint pjp) throws Throwable {
        System.out.println("beforeMethod");
    }

    @AfterReturning(value = "pt()", returning = "o")
    public void afterReturningMethod(JoinPoint pjp, Object o) throws Throwable {
        System.out.println("afterReturningMethod");
        System.out.println(o);
    }

    @After("pt()")
    public void afterMethod(JoinPoint pjp) throws Throwable {
        System.out.println("afterMethod");
    }

    @AfterThrowing(value = "pt()", throwing = "ex")
    public void afterThrowing(JoinPoint pjp, Throwable ex) throws Throwable {
        System.out.println("afterThrowing");
        System.out.println(ex);
    }

}
